package com.example.sprintbootarithmetic.DataStructure.joseful;

public class CirleSingleLindedList {
    private Boy first = null;

    public void addBoy(int nums) {
        if (nums < 1) {
            System.out.println("当前节点数量不正确");
            return;
        }
        Boy curBoy = null;
        for (int i = 1; i <= nums; i++) {
            Boy boy = new Boy(i);
            if (i == 1) {
                first = boy;
                first.setNext(boy);
                curBoy = first;
            } else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }

    public void showBoy() {
        if (first == null) {
            System.out.println("当前节点为空");
            return;
        }
        Boy curboy = first;
        while (true) {
            System.out.println("当前是小孩第" + curboy.id + "号");
            if (curboy.getNext() == first) {
                return;
            }
            curboy = curboy.next;
        }
    }

    /**
     * 方法描述
     *
     * @param startNo  表示从第几个小孩开始数
     * @param countNum 表示数多少次
     * @param nums     表示当前有多少个小孩的数量
     * @description:
     * @return: void
     * @author: daixingyu
     * @date: 2021-05-12  11:50
     */
    public void countBoy(int startNo, int countNum, int nums) {
    /*    //判断当前界节点是否为空，如果为空则输入异常
        if (first == null || startNo < 1 || startNo > nums) {
            System.out.println("当前输入参数有误，请重新输入");
            return;
        }
        //需求创建一个辅助指针(变量) helper , 事先应该指向环形链表的最后这个节点
        Boy helper = first;
        while (true) {
            *//* System.out.println(helper.getNext().toString());*//*
            if (helper.getNext() == first) {
                break;
            }
            //helper指向下一个
            helper = helper.getNext();
        }
        //当小孩开始报数时，移动k-1次
        for (int i = 0; i < startNo - 1; i++) {
            first = first.next;
            helper = helper.next;
        }
        //开始数数
        while (true) {
            if (helper == first) { //说明圈中只有一个节点
                break;
            }
            //让first和header同时移动countNum-1,相当于指定移动countNum次
            for (int j = 0; j < countNum - 1; j++) {
                first = first.next;//把first的
                helper = helper.next;
            }
            System.out.printf("小孩%d出圈\n", first.id);
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后留在圈中的小孩编号%d \n", helper.id);*/
        if (startNo > 1 || startNo > nums || first == null) {
            System.out.println("输入的参数有误");
            return;
        }
        Boy helper = first;
        while (true) {
            if (helper.getNext() == first) {
                break;
            }
            helper = helper.next;
        }
        for (int i = 0; i < startNo - 1; i++) {
            first = first.next;
            helper = helper.next;
        }
        while (true) {
            //说明只有一个节点
            if (helper == first) {
                break;
            }
            for (int j = 0; j < countNum - 1; j++) {
                first = first.next;
                helper = helper.next;
            }
            System.out.printf("当前%d号小孩出圈\n", first.id);
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后出圈的小孩为%d", helper.id);
    }
}
